<%+header%>

<% if stderr and #stderr > 0 then %><pre class="error"><%=pcdata(stderr)%></pre><% end %>
<% 
    local latest_omr_version=luci.model.uci.cursor():get("openmptcprouter","latest_versions","vps")
    local ucic  = luci.model.uci.cursor()
    menuentry = ucic:get("openmptcprouter","settings","menu") or "openmptcprouter"
    omr_test = false
    uci:foreach("openmptcprouter","server", function(s)
	servername = s[".name"]
	local omr_version=luci.model.uci.cursor():get("openmptcprouter",servername,"omr_version")
	if omr_version ~= "" and latest_omr_version ~= "" and omr_version ~= latest_omr_version then
		omr_test = true
	end
    end)
    if omr_test == true then
%>
<form class="inline" method="post" action="<%=url('admin/system/' .. menuentry:lower() .. '/update_vps')%>">
    <div class="cbi-map">
	<h2 name="content"><%:Update VPS%></h2>
	<fieldset class="cbi-section" id="update">
	<div class="cbi-section-descr"><%:Update remotly the server to latest version when needed.%> <b><%:Beta%></b></div>
	<div class="cbi-value">
	    <label class="cbi-value-title"><%:Update server%></label>
	    <div class="cbi-value-field">
		<input type="hidden" name="token" value="<%=token%>" />
		<input type="hidden" name="server" value="<%=servername%>" />
		<input type="hidden" name="flash" value="1" />
		<input type="submit" name="update_vps" class="cbi-button cbi-button-apply" value="<%:Update%>">
	    </div>
	</div>
    </div>
</form>
<%
    end
%>

<hr />
<form class="inline" method="post" action="<%=url('admin/system/' .. menuentry:lower() .. '/settings_add')%>">
    <div class="cbi-map">
	<h2 name="content"><%:Advanced Settings%></h2>
	<fieldset class="cbi-section" id="networks">
	    <legend><%:VPS settings%></legend>
	    <%
		uci:foreach("openmptcprouter","server", function(s)
			servername = s[".name"]
	    %>
	    <h3><%=servername%></h3>
	    <div class="cbi-section-node">
		<div class="cbi-section-descr"></div>
		<div class="cbi-value">
		    <label class="cbi-value-title"><%:Redirects all ports from server to this router%></label>
		    <div class="cbi-value-field">
			<input type="hidden" name="server.<%=servername%>" value="<%=servername%>" />
			<input type="checkbox" name="redirect_ports.<%=servername%>" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter",servername,"redirect_ports") == "1" then %>checked<% end %>>
			<br />
			<div class="cbi-value-description">
			    <%:You shouldn't do that and you should redirect only needed ports%>
			</div>
		    </div>
		</div>
		<div class="cbi-value">
		    <label class="cbi-value-title"><%:Disable ports redirection defined in firewall from server to this router%></label>
		    <div class="cbi-value-field">
			<input type="hidden" name="server.<%=servername%>" value="<%=servername%>" />
			<input type="checkbox" name="nofwredirect.<%=servername%>" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter",servername,"nofwredirect") == "1" then %>checked<% end %>>
		    </div>
		</div>
	    </div>
	    <%
		end)
	    %>
	    <%
		local obfs_installed = nixio.fs.access("/usr/bin/obfs-local")
		local v2ray_installed = nixio.fs.access("/usr/bin/v2ray-plugin")
		if obfs_installed or v2ray_installed then
	    %>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Enable ShadowSocks Obfuscating%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="obfs" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("shadowsocks-libev","sss0","obfs") == "1" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Obfuscating will be enabled on both side%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Obfuscating plugin%></label>
		<div class="cbi-value-field">
		    <select class="cbi-input-select" name="obfs_plugin">
			<%
			    if v2ray_installed then
			%>
			<option value="v2ray" <% if luci.model.uci.cursor():get("shadowsocks-libev","sss0","obfs_plugin") == "v2ray" then %>selected="selected"<% end %>>v2ray</option>
			<%
			    end
			    if obfs_installed then
			%>
			<option value="obfs-simple" <% if luci.model.uci.cursor():get("shadowsocks-libev","sss0","obfs_plugin") == "obfs-simple" then %>selected="selected"<% end %>>simple-obfs</option>
			<%
			    end
			%>
		    </select>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Obfuscating type%></label>
		<div class="cbi-value-field">
		    <select class="cbi-input-select" name="obfs_type">
			<option value="http" <% if luci.model.uci.cursor():get("shadowsocks-libev","tracker","obfs_type") == "http" then %>selected="selected"<% end %>>http</option>
			<option value="tls" <% if luci.model.uci.cursor():get("shadowsocks-libev","tracker","obfs_type") == "tls" then %>selected="selected"<% end %>>tls</option>
		    </select>
		</div>
	    </div>
	    <%
		end
	    %>
	</fieldset>
	<hr />
	<fieldset class="cbi-section" id="networks">
	<legend><%:Networks settings%></legend>
	    <div class="cbi-section-descr"></div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:IPv4 TCP Keepalive time%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="tcp_keepalive_time" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("sysctl net.ipv4.tcp_keepalive_time")):match(" %d+"))%>">
		    <br />
		    <div class="cbi-value-description">
			<%:How often TCP sends out keepalive messages when keepalive is enabled.%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:IPv4 TCP FIN timeout%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="tcp_fin_timeout" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("sysctl net.ipv4.tcp_fin_timeout")):match(" %d+"))%>">
		    <br />
		    <div class="cbi-value-description">
			<%:The length of time an orphaned (no longer referenced by any application) connection will remain in the FIN_WAIT_2 state before it is aborted at the local end.%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:IPv4 TCP SYN retries%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="tcp_syn_retries" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("sysctl net.ipv4.tcp_syn_retries")):match(" %d+"))%>">
		    <br />
		    <div class="cbi-value-description">
			<%:Number of times initial SYNs for an active TCP connection attempt will be retransmitted.%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:IPv4 TCP SYN retries1%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="tcp_retries1" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("sysctl net.ipv4.tcp_retries1")):match(" %d+"))%>">
		    <br />
		    <div class="cbi-value-description">
			<%:This value influences the time, after which TCP decides, that something is wrong due to unacknowledged RTO retransmissions, and reports this suspicion to the network layer.%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:IPv4 TCP SYN retries2%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="tcp_retries2" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("sysctl net.ipv4.tcp_retries2")):match(" %d+"))%>">
		    <br />
		    <div class="cbi-value-description">
			<%:This value influences the timeout of an alive TCP connection, when RTO retransmissions remain unacknowledged.%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:IPv4 TCP Fast Open%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="tcp_fastopen" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("sysctl net.ipv4.tcp_fastopen")):match(" %d+"))%>">
		</div>
	    </div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:IPv4 IP default TTL%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="ip_default_ttl" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("sysctl net.ipv4.ip_default_ttl")):match(" %d+"))%>">
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Enable IPv6%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="enableipv6" class="cbi-input-checkbox" value="0" <% if luci.model.uci.cursor():get("openmptcprouter","settings","disable_ipv6") == "0" then %>checked<% end %>>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Disable external check%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="externalcheck" class="cbi-input-checkbox" value="0" <% if luci.model.uci.cursor():get("openmptcprouter","settings","external_check") == "0" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:When enable check are done on external sites to get each WAN IP and the IP used to go outside.%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Disable TCP Fast Open%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="disablefastopen" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter","settings","disable_fastopen") == "1" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Disable TCP Fast Open on Linux and Shadowsocks configuration%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Enable TCP Low Latency%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="enablenodelay" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter","settings","enable_nodelay") == "1" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Optimize for latency instead of bandwidth%>
		    </div>
		</div>
	    </div>
	</fieldset>
	<hr />
	<fieldset class="cbi-section" id="other">
	<legend><%:Other settings%></legend>
	    <div class="cbi-section-descr"></div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Save vnstats stats%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="savevnstat" class="cbi-input-checkbox" value="1" <% if luci.util.trim(luci.sys.exec("uci -q get openmptcprouter.settings.vnstat_backup")) == "1" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Save vnstats statistics on disk%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Disable gateway ping%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="disablegwping" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter","settings","disablegwping") == "1" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Disable gateway ping status check%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Disable interfaces auto rename%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="disableintfrename" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter","settings","disableintfrename") == "1" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Disable renaming interfaces%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:When proxy shadowsocks is used, use it for UDP if proxy down%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="shadowsocksudp" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter","settings","shadowsocksudp") == "1" then %>checked<% end %>>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Disable default gateway%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="disabledefaultgw" class="cbi-input-checkbox" value="0" <% if luci.model.uci.cursor():get("openmptcprouter","settings","defaultgw") == "0" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Disable default gateway, no internet if VPS are down%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Disable server ping%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="disableserverping" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter","settings","disableserverping") == "1" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Disable server ping status check%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Disable tracebox test%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="disabletracebox" class="cbi-input-checkbox" value="0" <% if luci.model.uci.cursor():get("openmptcprouter","settings","tracebox") == "0" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Disable multipath test using tracebox%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Debug%></label>
		<div class="cbi-value-field">
		    <input type="checkbox" name="debug" class="cbi-input-checkbox" value="1" <% if luci.model.uci.cursor():get("openmptcprouter","settings","debug") == "1" then %>checked<% end %>>
		    <br />
		    <div class="cbi-value-description">
			<%:Enable debug logs%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Master interface selection%></label>
		<div class="cbi-value-field">
		    <select class="cbi-input-select" name="master_type">
			<option value="change" <% if luci.model.uci.cursor():get("openmptcprouter","settings","master") == "change" then %>selected="selected"<% end %>><%:On wizard change%></option>
			<option value="dynamic" <% if luci.model.uci.cursor():get("openmptcprouter","settings","master") == "dynamic" then %>selected="selected"<% end %>><%:Dynamic change%></option>
			<option value="static" <% if luci.model.uci.cursor():get("openmptcprouter","settings","master") == "static" then %>selected="selected"<% end %>><%:No change%></option>
			<option value="balancing" <% if luci.model.uci.cursor():get("openmptcprouter","settings","master") == "balancing" then %>selected="selected"<% end %>><%:Balancing%></option>
		    </select>
		</div>
	    </div>
	</fieldset>
	<% if nixio.fs.access("/sys/devices/system/cpu/cpufreq/policy0/scaling_min_freq") then %>
	<fieldset class="cbi-section" id="system">
	<legend><%:Systems settings%></legend>
	    <div class="cbi-section-descr"></div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:Minimum scaling CPU frequency%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="scaling_min_freq" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("cat /sys/devices/system/cpu/cpufreq/policy0/scaling_min_freq")):match("%d+"))%>">
		</div>
	    </div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:Maximum scaling CPU frequency%></label>
	        <div class="cbi-value-field">
		    <input type="text" name="scaling_max_freq" class="cbi-input-text" value="<%=tonumber((luci.sys.exec("cat /sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq")):match("%d+"))%>">
		</div>
	    </div>
	    <div class="cbi-value">
	        <label class="cbi-value-title"><%:Scaling governor%></label>
	        <div class="cbi-value-field">
		    <select class="cbi-input-select" name="scaling_governor">
			<% 
			    governor=luci.util.trim(luci.sys.exec("cat /sys/devices/system/cpu/cpufreq/policy0/scaling_governor"))
			    available_governors=luci.sys.exec("cat /sys/devices/system/cpu/cpufreq/policy0/scaling_available_governors")
			    for gov in string.gmatch(available_governors, "[^%s]+") do
			%>
			<option value="<%=gov%>" <% if governor == gov then %>selected="selected"<% end %>><%=gov%></option>
			<% 
			    end
			%>
		    </select>
		</div>
	    </div>
	</fieldset>
	<% end %>
    </div>
    <div class="cbi-page-actions">
	<input type="hidden" name="token" value="<%=token%>" />
	<input class="cbi-button cbi-button-apply" type="submit" value="<%:Save & Apply%>" /> <input class="cbi-button cbi-button-reset" type="button" value="Reset" onclick="location.href='<%=url('admin/system/openmptcprouter/settings')%>'" />
    </div>
</form>
<%+footer%>
